<?php



namespace App\Http\Controllers\Api\Order;


use App\Http\Controllers\Api\BasicController;
use App\Models\OrderAppoint;
use App\Models\SiteCode;
use App\Models\User;
use App\Models\UserFocus;
use App\Models\UserInfo;
use App\Services\Sms;
use GatewayWorker\Lib\Gateway;
use Illuminate\Support\Facades\DB;
use Jiannei\Response\Laravel\Support\Facades\Response;
use Label84\HoursHelper\Facades\HoursHelper;

class OrderController extends BasicController
{

    /**
     * @api {post} /order/appoint 预约&支付
     * @apiDescription
     * @apiHeader {String} Authorization='Bearer '
     */
    public function place()
    {
        $data=$this->data(['appoint_id', 'day','consult_type','consult_way','buy_time','appoint_time','order_type']);
        $data['user_id']=$this->uid();
        try {
            DB::beginTransaction();
            $order = (new OrderAppoint)->createOrder($data);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return Response::fail($e->getMessage());
        }
        return Response::success($order);
    }



    public function accept(){
        $uid=$this->uid();
        $oid=$this->request->input('oid',);
        $result=(new OrderAppoint())->accept($uid,$oid);
        return Response::success($result);

    }

    public function cancel(){
        $uid=$this->uid();
        $oid=$this->request->input('oid',);
        $result=(new OrderAppoint())->cancel($uid,$oid);
        return Response::success($result);

    }
    public function confirm(){
        $uid=$this->uid();
        $oid=$this->request->input('oid',);
        $duration=$this->request->input('duration');
        $result=(new OrderAppoint())->confirm($uid,$oid,$duration);
        return Response::success($result);

    }

    public function rate(){
        $uid=$this->uid();
        $oid=$this->request->input('oid',);
        $comment=$this->request->input('comment','');
        $grade=$this->request->input('grade',5);
        $result=(new OrderAppoint())->rate($uid,$oid,$comment,$grade);
        return Response::success($result);

    }

    public function list(){
        $uid=$this->uid();
        $status=$this->request->input('status',1);
        $result=(new OrderAppoint())->list($uid,$status);
        return Response::success($result);
    }


    public function appointed(){
        $day=$this->request->input('day');
        $result=OrderAppoint::select(['appoint_time',DB::raw('count(id) as num')])->where(['day'=>$day])->whereIn('status',[0,1])->groupBy('appoint_time')->get();
        return Response::success($result);
    }

    public function charging(){
        $uid=$this->uid();
        $oid=$this->request->input('oid');
        try {
            DB::beginTransaction();
            $result = (new OrderAppoint())->charging($uid, $oid);
            DB::commit();
        }catch (\Exception $e){
            DB::rollBack();
            return Response::fail($e->getMessage());
        }
        return Response::success($result);

    }



}
